package com.qyer.util.log.util;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class StatisticsUtils {

	static HashMap<String, String> cateName = new HashMap<String, String>();
	static HashMap<String, String> areaName = new HashMap<String, String>();

	public List<String> id2Name(String idType, String str) {
		String[] strArr = str.split(",");
		List<String> nameList = new ArrayList<String>();
		if (strArr.length < 2) {
			return nameList;
		} else if (strArr.length == 2) {
			if ("cate".equalsIgnoreCase(idType)) {
				String cate1 = cateName.get(strArr[1]);
				cate1 = cate1 == null ? strArr[1] : cate1;
				nameList.add(cate1 + "@" + "A" + "@" + "A");
			} else if ("area".equalsIgnoreCase(idType)) {
				String area1 = areaName.get(strArr[1]);
				area1 = area1 == null ? strArr[1] : area1;
				nameList.add(area1 + "@" + "A" + "@" + "A");
			}

		} else if (strArr.length == 3) {
			if ("cate".equalsIgnoreCase(idType)) {
				String cate1 = cateName.get(strArr[1]);
				cate1 = cate1 == null ? strArr[1] : cate1;
				String cate2 = cateName.get(strArr[2]);
				cate2 = cate2 == null ? strArr[2] : cate2;
				nameList.add(cate1 + "@" + cate2 + "@" + "A");
				nameList.add(cate1 + "@" + "A" + "@" + "A");
			} else if ("area".equalsIgnoreCase(idType)) {
				String area1 = areaName.get(strArr[1]);
				area1 = area1 == null ? strArr[1] : area1;
				String area2 = areaName.get(strArr[2]);
				area2 = area2 == null ? strArr[2] : area2;
				nameList.add(area1 + "@" + area2 + "@" + "A");
				nameList.add(area1 + "@" + "A" + "@" + "A");
			}

		} else {
			if ("cate".equalsIgnoreCase(idType)) {
				String cate1 = cateName.get(strArr[1]);
				cate1 = cate1 == null ? strArr[1] : cate1;
				String cate2 = cateName.get(strArr[2]);
				cate2 = cate2 == null ? strArr[2] : cate2;
				String cate3 = cateName.get(strArr[3]);
				cate3 = cate3 == null ? strArr[3] : cate3;
				nameList.add(cate1 + "@" + cate2 + "@" + cate3);
				nameList.add(cate1 + "@" + cate2 + "@" + "A");
				nameList.add(cate1 + "@" + "A" + "@" + "A");
			} else if ("area".equalsIgnoreCase(idType)) {
				String area1 = areaName.get(strArr[1]);
				area1 = area1 == null ? strArr[1] : area1;
				String area2 = areaName.get(strArr[2]);
				area2 = area2 == null ? strArr[2] : area2;
				String area3 = areaName.get(strArr[3]);
				area3 = area3 == null ? strArr[3] : area3;
				nameList.add(area1 + "@" + area2 + "@" + area3);
				nameList.add(area1 + "@" + area2 + "@" + "A");
				nameList.add(area1 + "@" + "A" + "@" + "A");
			}
		}
		return nameList;
	}
	
	/**
	 * 三级城市维度
	 * 
	 * */
	public static List<String> getDim3AreaList(String str) {
		String[] strArr = str.split(",");
//		for(int i =0 ; i < strArr.length ; i++){
//			if(strArr[i]==null||!strArr[i].matches("^[0-9]*[1-9][0-9]*$")){
//				strArr[i]="nonumber";
//			}
//		}
		
		List<String> dimList = new ArrayList<String>();
		if (strArr.length < 1) {
			dimList.add("A" + "@" + "A" + "@" + "A");
			return dimList;
		} else if (strArr.length == 1) {
			String cate1 = strArr[0];
			cate1 = cate1 == null ? strArr[0] : cate1;
			dimList.add(cate1 + "@" + "A" + "@" + "A");
			dimList.add("A" + "@" + "A" + "@" + "A");
		} else if (strArr.length == 2) {
			String cate1 = strArr[0];
			cate1 = cate1 == null ? strArr[0] : cate1;
			String cate2 = strArr[1];
			cate2 = cate2 == null ? strArr[1] : cate2;
			dimList.add(cate1 + "@" + cate2 + "@" + "A");
			dimList.add(cate1 + "@" + "A" + "@" + "A");
			dimList.add("A" + "@" + "A" + "@" + "A");
		}else if (strArr.length == 3) {
			String cate1 = strArr[0];
			cate1 = cate1 == null ? strArr[0] : cate1;
			String cate2 = strArr[1];
			cate2 = cate2 == null ? strArr[1] : cate2;
			String cate3 = strArr[2];
			cate3 = cate3 == null ? strArr[2] : cate3;
			dimList.add(cate1 + "@" + cate2 + "@" + cate3);
			dimList.add(cate1 + "@" + cate2 + "@" + "A");
			dimList.add(cate1 + "@" + "A" + "@" + "A");
			dimList.add("A" + "@" + "A" + "@" + "A");
		} else {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			String cate2 = strArr[2];
			cate2 = cate2 == null ? strArr[2] : cate2;
			String cate3 = strArr[3];
			cate3 = cate3 == null ? strArr[3] : cate3;
			dimList.add(cate1 + "@" + cate2 + "@" + cate3);
			dimList.add(cate1 + "@" + cate2 + "@" + "A");
			dimList.add(cate1 + "@" + "A" + "@" + "A");
			dimList.add("A" + "@" + "A" + "@" + "A");
		}
		return dimList;
	}
	/**
	 * 二级城市维度
	 * 
	 * */
	public static List<String> getDim2AreaList(String str) {
		String[] strArr = str.split(",");
		for(int i =0 ; i < strArr.length ; i++){
			if(strArr[i]==null||!strArr[i].matches("^[0-9]*[1-9][0-9]*$")){
				strArr[i]="nonumber";
			}
		}
		
		List<String> dimList = new ArrayList<String>();
		if (strArr.length < 2) {
			dimList.add("A" + "@" + "A");
			return dimList;
		} else if (strArr.length == 2) {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			dimList.add(cate1 + "@" + "A" );
			dimList.add("A" + "@" + "A" );
		} else if (strArr.length == 3) {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			String cate2 = strArr[2];
			cate2 = cate2 == null ? strArr[2] : cate2;
			dimList.add(cate1 + "@" + cate2 );
			dimList.add(cate1 + "@" + "A" );
			dimList.add("A" + "@" + "A" );
		} else {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			String cate2 = strArr[2];
			cate2 = cate2 == null ? strArr[2] : cate2;
			dimList.add(cate1 + "@" + cate2 );
			dimList.add(cate1 + "@" + "A" );
			dimList.add("A" + "@" + "A" );
		}
		return dimList;
	}
	/**
	 * 
	 * 五级类别
	 * **/
	public static List<String> getDim5CateList(String str) {
		String[] strArr = str.split(",");
		List<String> dimList = new ArrayList<String>();
		
		if (strArr.length < 2) {
			dimList.add("A" + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
			return dimList;
		} else if (strArr.length == 2) {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			dimList.add(cate1 + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
			dimList.add("A" + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
		} else if (strArr.length == 3) {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			String cate2 = strArr[2];
			cate2 = cate2 == null ? strArr[2] : cate2;
			dimList.add(cate1 + "@" + cate2 + "@" + "A"+"@" + "A"+"@" + "A");
			dimList.add(cate1 + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
			dimList.add("A" + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
		} else if (strArr.length == 4) {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			String cate2 = strArr[2];
			cate2 = cate2 == null ? strArr[2] : cate2;
			String cate3 = strArr[3];
			cate3 = cate3 == null ? strArr[3] : cate3;
			dimList.add(cate1 + "@" + cate2 + "@" + cate3+"@" + "A"+"@" + "A");
			dimList.add(cate1 + "@" + cate2 + "@" + "A"+"@" + "A"+"@" + "A");
			dimList.add(cate1 + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
			dimList.add("A" + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
		}else if (strArr.length == 5) {
				String cate1 = strArr[1];
				cate1 = cate1 == null ? strArr[1] : cate1;
				String cate2 = strArr[2];
				cate2 = cate2 == null ? strArr[2] : cate2;
				String cate3 = strArr[3];
				cate3 = cate3 == null ? strArr[3] : cate3;
				String cate4 = strArr[4];
				cate4 = cate4 == null ? strArr[4] : cate4;
				
				dimList.add(cate1 + "@" + cate2 + "@" + cate3+"@" + cate4+"@" + "A");
				dimList.add(cate1 + "@" + cate2 + "@" + cate3+"@" + "A"+"@" + "A");
				dimList.add(cate1 + "@" + cate2 + "@" + "A"+"@" + "A"+"@" + "A");
				dimList.add(cate1 + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
				dimList.add("A" + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
			}else  {
				String cate1 = strArr[1];
				cate1 = cate1 == null ? strArr[1] : cate1;
				String cate2 = strArr[2];
				cate2 = cate2 == null ? strArr[2] : cate2;
				String cate3 = strArr[3];
				cate3 = cate3 == null ? strArr[3] : cate3;
				String cate4 = strArr[4];
				cate4 = cate4 == null ? strArr[4] : cate4;
				String cate5 = strArr[5];
				cate5 = cate5 == null ? strArr[5] : cate5;
				dimList.add(cate1 + "@" + cate2 + "@" + cate3+"@" + cate4+"@" + cate5);
				dimList.add(cate1 + "@" + cate2 + "@" + cate3+"@" + cate4+"@" + "A");
				dimList.add(cate1 + "@" + cate2 + "@" + cate3+"@" + "A"+"@" + "A");
				dimList.add(cate1 + "@" + cate2 + "@" + "A"+"@" + "A"+"@" + "A");
				dimList.add(cate1 + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
				dimList.add("A" + "@" + "A" + "@" + "A"+"@" + "A"+"@" + "A");
			}
		
		if(strArr.length>=3){
			//自定义类型 自定义组合租房
			if(strArr[2].equals("8") || strArr[2].equals("9") || strArr[2].equals("10") || strArr[2].equals("11") || strArr[2].equals("37031")){
				dimList.add("1" + "@" + "-101" + "@" + "A"+"@" + "A"+"@" + "A");
			}else if (strArr[2].equals("13") || strArr[2].equals("14") || strArr[2].equals("15")){
			//自定义组合商业房产
				dimList.add("1" + "@" + "-102" + "@" + "A"+"@" + "A"+"@" + "A");
			}
		}
		return dimList;
	}
	
	/**
	 * 
	 * 三级类别
	 * **/
	public static List<String> getDim3CateList(String str) {
		String[] strArr = str.split(",");
		List<String> dimList = new ArrayList<String>();
		
		if (strArr.length < 2) {
			dimList.add("A" + "@" + "A" + "@" + "A");
			return dimList;
		} else if (strArr.length == 2) {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			dimList.add(cate1 + "@" + "A" + "@" + "A");
			dimList.add("A" + "@" + "A" + "@" + "A");
		} else if (strArr.length == 3) {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			String cate2 = strArr[2];
			cate2 = cate2 == null ? strArr[2] : cate2;
			dimList.add(cate1 + "@" + cate2 + "@" + "A");
			dimList.add(cate1 + "@" + "A" + "@" + "A");
			dimList.add("A" + "@" + "A" + "@" + "A");
		} else if (strArr.length >= 4) {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			String cate2 = strArr[2];
			cate2 = cate2 == null ? strArr[2] : cate2;
			String cate3 = strArr[3];
			cate3 = cate3 == null ? strArr[3] : cate3;
			dimList.add(cate1 + "@" + cate2 + "@" + cate3);
			dimList.add(cate1 + "@" + cate2 + "@" + "A");
			dimList.add(cate1 + "@" + "A" + "@" + "A");
			dimList.add("A" + "@" + "A" + "@" + "A");
		}
		
		if(strArr.length>=3){
			//自定义类型 自定义组合租房
			if(strArr[2].equals("8") || strArr[2].equals("9") || strArr[2].equals("10") || strArr[2].equals("11") || strArr[2].equals("37031")){
				dimList.add("-101" + "@" + "A" + "@" + "A");
			}else if (strArr[2].equals("13") || strArr[2].equals("14") || strArr[2].equals("15")){
			//自定义组合商业房产
				dimList.add("-102" + "@" + "A" + "@" + "A");
			}
		}
		return dimList;
	}
	
	public static List<String> getDimListDD(String str) {
		String[] strArr = str.split(",");
		List<String> dimList = new ArrayList<String>();
		if (strArr.length < 2) {
			dimList.add("A" + "\t" + "A" + "\t" + "A");
			return dimList;
		} else if (strArr.length == 2) {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			dimList.add(cate1 + "\t" + "A" + "\t" + "A");
			dimList.add("A" + "\t" + "A" + "\t" + "A");
		} else if (strArr.length == 3) {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			String cate2 = strArr[2];
			cate2 = cate2 == null ? strArr[2] : cate2;
			dimList.add(cate1 + "\t" + cate2 + "\t" + "A");
			dimList.add(cate1 + "\t" + "A" + "\t" + "A");
			dimList.add("A" + "\t" + "A" + "\t" + "A");
		} else {
			String cate1 = strArr[1];
			cate1 = cate1 == null ? strArr[1] : cate1;
			String cate2 = strArr[2];
			cate2 = cate2 == null ? strArr[2] : cate2;
			String cate3 = strArr[3];
			cate3 = cate3 == null ? strArr[3] : cate3;
			dimList.add(cate1 + "\t" + cate2 + "\t" + cate3);
			dimList.add(cate1 + "\t" + cate2 + "\t" + "A");
			dimList.add(cate1 + "\t" + "A" + "\t" + "A");
			dimList.add("A" + "\t" + "A" + "\t" + "A");
		}
		return dimList;
	}
	
	public static List<String> getDimListDSource(String str) {
		String[] strArr = str.split("\t");
		List<String> dimList = new ArrayList<String>();
		if (strArr.length ==0) {
			dimList.add("A" + "@" + "A" );
			return dimList;
		} else if (strArr.length == 1) {
			String cate1 = strArr[0];
			cate1 = cate1 == null ? strArr[0] : cate1;
			dimList.add(cate1 + "@" + "A" );
			dimList.add("A" + "@" + "A" );
		} else if (strArr.length ==2) {
			String cate1 = strArr[0];
			String cate2 = strArr[1];
			cate1 = cate1 == null ? strArr[0] : cate1;
			cate2 = cate2 == null ? strArr[1] : cate2;
			dimList.add(cate1 + "@" + cate2 );
			dimList.add(cate1 + "@" + "A" );
			dimList.add("A" + "@" + "A" );
		} 
		return dimList;
	}
	
	
	
	public static List<String> getDimListTSource(String str) {
		String[] strArr = str.split("@");
		List<String> dimList = new ArrayList<String>();
		if (strArr.length ==0) {
			dimList.add("A@A" );
			return dimList;
		} else if (strArr.length == 1) {
			String cate1 = strArr[0];
			cate1 = cate1 == null ? strArr[0] : cate1;
			StringBuilder sb = new StringBuilder(cate1);
			sb.append("@A");
			dimList.add(sb.toString() );
			dimList.add("A@A" );
		} else if (strArr.length ==2) {
			String cate1 = strArr[0];
			String cate2 = strArr[1];
			cate1 = cate1 == null ? strArr[0] : cate1;
			cate2 = cate2 == null ? strArr[1] : cate2;
			StringBuilder sb = new StringBuilder(cate1);
			sb.append("@A");
			dimList.add(sb.toString() );
			
			StringBuilder sb2 = new StringBuilder(cate1);
			sb2.append("@").append(cate2);
			dimList.add(sb2.toString() );
			
			dimList.add("A@A" );
		} else {
			String cate1 = strArr[0];
			String cate2 = strArr[1];
			cate1 = cate1 == null ? strArr[0] : cate1;
			cate2 = cate2 == null ? strArr[1] : cate2;
			StringBuilder sb = new StringBuilder(cate1);
			sb.append("@A");
			dimList.add(sb.toString() );
			
			StringBuilder sb2 = new StringBuilder(cate1);
			sb2.append("@").append(cate2);
			dimList.add(sb2.toString() );
			dimList.add("A@A" );
		}
		
		return dimList;
	}

	public static Path[] getRecursivePaths(FileSystem fs, String basePath) throws IOException,
			URISyntaxException {
		List<Path> result = new ArrayList<Path>();
		basePath = fs.getUri() + basePath;
		FileStatus[] listStatus = fs.globStatus(new Path(basePath + "/*"));
		for (FileStatus fstat : listStatus) {
			readSubDirectory(fstat, basePath, fs, result);
		}
		return (Path[]) result.toArray(new Path[result.size()]);
	}

	private static void readSubDirectory(FileStatus fileStatus, String basePath, FileSystem fs,
			List<Path> paths) throws IOException, URISyntaxException {
		if (!fileStatus.isDir()) {
			paths.add(fileStatus.getPath());
		} else {
			String subPath = fileStatus.getPath().toString();
			FileStatus[] listStatus = fs.globStatus(new Path(subPath + "/*"));
			if (listStatus.length == 0) {
				paths.add(fileStatus.getPath());
			}
			for (FileStatus fst : listStatus) {
				readSubDirectory(fst, subPath, fs, paths);
			}
		}
	}

	public static void main(String[] args) {
		String string ="1\t1";
		List<String> s =getDimListDSource(string);
		for (String ss : s) {
			System.out.println(ss);
		}
	}

}
